package cur.nature.webservice.common;

import cur.nature.webservice.ws.UserService;
import cur.nature.webservice.ws.UserService2;
import cur.nature.webservice.ws.UserServiceImpl;
import cur.nature.webservice.ws.UserServiceImpl2;
import jakarta.xml.ws.Endpoint;
import lombok.extern.slf4j.Slf4j;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Configuration
public class CXFConfig {

    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }

    @Bean(name = "wsBean")
    public ServletRegistrationBean dispatcherServlet() {
        ServletRegistrationBean wbsServlet = new ServletRegistrationBean(new CXFServlet(), "/api/*");
//        wbsServlet.setUrlMappings(Arrays.asList("/api?wsdl"));
        wbsServlet.setLoadOnStartup(10);
        wbsServlet.setEnabled(true);
        return wbsServlet;
    }

    @Bean(name = "wsBean2")
    public ServletRegistrationBean dispatcherServlet2() {
        ServletRegistrationBean wbsServlet = new ServletRegistrationBean(new CXFServlet(), "/ws/*");
//        wbsServlet.setUrlMappings(Arrays.asList("/api?wsdl"));
        wbsServlet.setLoadOnStartup(10);
        wbsServlet.setEnabled(true);
        return wbsServlet;
    }

    @Bean
    public UserService userService() {
        return new UserServiceImpl();
    }

    @Bean
    public UserService2 userService2() {
        return new UserServiceImpl2();
    }

    @Bean
    public Endpoint endpointPurchase() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), userService());
//        endpoint.publish("/api");

        // change namcespacee prefx
//        Map<String, Object> prop = new HashMap<>();
//        Map<String, String> header = new HashMap<>();
//        header.put("soapenv","http://schemas.xmlsoap.org/soap/envelope/");
//        prop.put("soap.env.ns.map", header);
        // prop.put("disable.outputstream.optimization", true);
//        endpoint.setProperties(prop);

        endpoint.publish();
//        System.out.println("服务发布成功！地址为：http://localhost:8080/ws/api?wsdl");
        return endpoint;
    }

    @Bean
    public Endpoint endpointPurchase2() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), userService2());
        endpoint.publish("/aaa");

        // change namcespacee prefx
//        Map<String, Object> prop = new HashMap<>();
//        Map<String, String> header = new HashMap<>();
//        header.put("soapenv","http://schemas.xmlsoap.org/soap/envelope/");
//        prop.put("soap.env.ns.map", header);
        // prop.put("disable.outputstream.optimization", true);
//        endpoint.setProperties(prop);

//        System.out.println("服务发布成功！地址为：http://localhost:8080/ws/api?wsdl");
        return endpoint;
    }
}

